package com.briup.web_demo2.response;

import com.briup.web_demo2.enums.ResponseEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Schema(name = "统一响应类")

public class Response <T> {
    //响应状态码
    @Schema(name = "code",title="响应状态码")
    private Integer code;
    //响应信息
    @Schema(name = "message",title="响应信息")
    private String message;
    //响应数据
    @Schema(name = "data",title="响应数据")
    private T data;

    //空参构造器
    private Response(){
    }
    /**
     * @Description : 成功方法（不需要返回值时使用）
     * @Date : 2024/09/25 14:06:18
     * @Author : qinyc     * @see Response
     */
    public static Response success(){
        //创建统一响应对象
        Response response = new Response();
        //设置成功响应状态码
        response.setCode(ResponseEnum.SUCCESS.getCode());
        //设置成功的响应信息
        response.setMessage(ResponseEnum.SUCCESS.getMessage());
        //返回响应对象
        return response;
    }

    /**
     * @Description : 成功方法（需要传入响应数据的值）
     * @Date : 2024/09/25 14:06:59
     * @Author : qinyc     * @see T 响应数据
     * @see Response<T>
     */
    public static <T> Response<T> success(T data){
        Response response = success();
        //设置响应数据
        response.setData(data);
        //返回响应
        return response;
    }

    /**
     * @Description : 错误方法（系统中不能预判到的错误）
     * @Date : 2024/09/25 14:08:09
     * @Author : qinyc     * @see Response
     */
    public static Response error(){
        Response response = new Response();
        response.setCode(ResponseEnum.ERROR.getCode());
        response.setMessage(ResponseEnum.ERROR.getMessage());
        return response;
    }

    /**
     * @Description : 错误方法（系统考虑到的错误）
     * @Date : 2024/09/25 14:09:15
     * @Author : qinyc     * @see ResponseEnum 统一响应枚举
     * @see Response
     */
    public static Response error(ResponseEnum responseEnum){
        Response response = new Response();
        response.setCode(responseEnum.getCode());
        response.setMessage(responseEnum.getMessage());
        return response;
    }
}
